#include "message.hpp"
#include "net.hpp"
#include "dispatcher.hpp"
void onRpcRequest (const bitrpc::BaseConnection::ptr& conn, bitrpc::RpcRequest::ptr& msg){
    std::cout << "收到了rpc请求"<<msg->method();
    std::string body=msg->serialize();
    std::cout<<body<<std::endl;
    auto rpc_req=bitrpc::MessageFactory::create<bitrpc::RpcResponse>();
    rpc_req->setId("1111");
    rpc_req->setMType(bitrpc::MType::RSP_RPC);
    rpc_req->setRCode(bitrpc::RCode::RCODE_OK);
    rpc_req->setResult(33);
    conn->send( rpc_req);
}
void onTopicRequest (const bitrpc::BaseConnection::ptr& conn, bitrpc::TopicRequest::ptr& msg){
    std::cout << "收到了topic请求";
    std::string body=msg->serialize();
    std::cout<<body<<std::endl;
    auto rpc_req=bitrpc::MessageFactory::create<bitrpc::TopicResponse>();
    rpc_req->setId("1111");
    rpc_req->setMType(bitrpc::MType::RSP_TOPIC);
    rpc_req->setRCode(bitrpc::RCode::RCODE_OK);
    conn->send( rpc_req);
}
int main()
{
    auto dispather= std::make_shared<bitrpc::Dispather> ();
    dispather->registerHander<bitrpc::RpcRequest>(bitrpc::MType::REQ_RPC,onRpcRequest);
    dispather->registerHander<bitrpc::TopicRequest>(bitrpc::MType::REQ_TOPIC,onTopicRequest);


    auto server=bitrpc::ServerFactory::create(9090);
    auto message_cb=std::bind(&bitrpc::Dispather::onMessage,dispather.get(),std::placeholders::_1,std::placeholders::_2);
    server->setMessageCallback(message_cb);
    server->start();
    return 0;
}
